package homework.newenergycharging.WorkALL;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class CombinedWritable implements Writable {
    private LongWritable count;       // 用于计数
    private DoubleWritable value1;    // 用于存储 keys[7] 的值
    private DoubleWritable value2;    // 用于存储 keys[1] 的值

    // 默认构造函数
    public CombinedWritable() {
        this.count = new LongWritable(0);
        this.value1 = new DoubleWritable(0.0);
        this.value2 = new DoubleWritable(0.0);
    }

    // 带参构造函数
    public CombinedWritable(LongWritable count, DoubleWritable value1, DoubleWritable value2) {
        this.count = count;
        this.value1 = value1;
        this.value2 = value2;
    }

    // Getter 和 Setter 方法
    public LongWritable getCount() {
        return count;
    }

    public void setCount(LongWritable count) {
        this.count = count;
    }

    public DoubleWritable getValue1() {
        return value1;
    }

    public void setValue1(DoubleWritable value1) {
        this.value1 = value1;
    }

    public DoubleWritable getValue2() {
        return value2;
    }

    public void setValue2(DoubleWritable value2) {
        this.value2 = value2;
    }

    // 序列化方法
    @Override
    public void write(DataOutput out) throws IOException {
        count.write(out);
        value1.write(out);
        value2.write(out);
    }

    // 反序列化方法
    @Override
    public void readFields(DataInput in) throws IOException {
        count.readFields(in);
        value1.readFields(in);
        value2.readFields(in);
    }

    // 重写 toString 方法
    @Override
    public String toString() {
        return count.get() + "," + value1.get() + "," + value2.get();
    }
}